package com.yht.org.service.impl;

import com.yht.basic.util.QueryResult;
import com.yht.org.domain.Employee;
import com.yht.org.mapper.EmployeeMapper;
import com.yht.org.query.EmployeeQuery;
import com.yht.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    @Transactional
    public void add(Employee employee) {
        employeeMapper.insert(employee);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        employeeMapper.delete(id);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

    @Override
    public Employee findById(Long id) {
        return employeeMapper.selectById(id);
    }

    @Override
    public List<Employee> findAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public QueryResult<Employee> findByEmployeeQuery(EmployeeQuery employeeQuery) {
        Integer total = employeeMapper.selectCount(employeeQuery);
        if(total == 0){
            return new QueryResult<>();
        }
        List<Employee> employees = employeeMapper.selectByEmployeeQuery(employeeQuery);
        QueryResult<Employee> result = new QueryResult<>(total, employees);
        return result;
    }
}
